//
//  AzirRPCService.User.swift
//  Center_RPC
//
//  Created by 蒋晨成 on 2022/1/17.
//

import Center_Core_Model
import Foundation
import GRPC

public extension AzirRPCService {
    func getUserProfile(
        targetGuid: String,
        complete: @escaping (Result<Azir_Gateway_GetUserProfileResponse, Error>) -> Void
    ) {
        requestQueue.async { [self] in
            let client = Azir_Gateway_UserClient(channel: self.connection, defaultCallOptions: self.callOptions(), interceptors: AzirClientInterceptorFactory())
            let call = client.getUserProfile(
                .with {
                    $0.guid = context.guid
                    $0.targetGuid = targetGuid
                })
            call.response.whenComplete { result in self.resultQueue.async { complete(result) } }
            do {
                let status = try call.status.wait()
                self.logger.info("Call Status : \(status)")
            } catch {
                self.logger.error("Call Failed With Error : \(error)")
            }
        }
    }

    func updateBasicProfile(basicProfile: Azir_BasicProfile, keys: [String], complete: @escaping (Result<Azir_Gateway_UpdateBasicProfileResponse, Error>) -> Void) {
        requestQueue.async { [self] in
            let client = Azir_Gateway_UserClient(channel: self.connection, defaultCallOptions: self.callOptions(), interceptors: AzirClientInterceptorFactory())
            let call = client.updateBasicProfile(
                .with {
                    $0.basicProfile = basicProfile
                    $0.keys = keys
                    $0.guid = context.guid
                })
            call.response.whenComplete { result in
                self.resultQueue.async { complete(result) }
            }
            do {
                let status = try call.status.wait()
                self.logger.info("Call Status : \(status)")
            } catch {
                self.logger.error("Call Failed With Error : \(error)")
            }
        }
    }
}
